package com.example.demo.service;

import com.example.demo.commom.AjaxResult;
import com.example.demo.mapper.ArtMapper;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.model.dto.comment.OpCommentDto;
import com.example.demo.model.dto.comment.OpCommentListDto;
import com.example.demo.model.dto.comment.PCommentDto;
import com.example.demo.model.dto.comment.SCommentDto;
import com.example.demo.model.entity.PComment;
import com.example.demo.model.entity.SComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2024-05-29
 * Time:23:11
 */
@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ArtMapper artMapper;
    public Integer pubPComment(Integer userId, PCommentDto dto){
        Integer res = commentMapper.pubPComment(userId, dto.getContent(), dto.getTarget_id(), System.currentTimeMillis());
        return res;
    }

    public Integer pubSComment(Integer userId, SCommentDto dto){
        SComment s = new SComment();
        s.setParentId(dto.getParent_id());
        s.setPostId(userId);
        s.setTargetId(dto.getTarget_id());
        s.setContent(dto.getContent());
        s.setCtime(System.currentTimeMillis());
        Integer res = commentMapper.pubSComment(s);
        return res;
    }
    public List<PComment> listPComment(Integer start, Integer limit, Integer id){
        return commentMapper.listPComment(start, limit, id);
    }
    public List<SComment> listSComment(Integer start, Integer limit, Integer id){
        return commentMapper.listSComment(start, limit, id);
    }
    public Integer delPComment(Integer userId, OpCommentDto dto){
        //检查是否有删除权限
        PComment pComment = commentMapper.PComment(dto.getId());
        if(pComment == null){
            return -2;
        }
        // 当前发送删除请求的人不是发布评论的人
        if(pComment.getUserId() != userId){
            // 验证当前发送删除请求的人是不是art的作者
            Integer artUserId = artMapper.userIdByArt(pComment.getArtId());
            if(artUserId != userId){
                return -1;
            }
        }
        return commentMapper.delPComment(dto.getId());
    }
    public Integer delSComment(Integer userId, OpCommentDto dto){
        //检查是否有删除权限
        SComment sComment = commentMapper.SComment(dto.getId());
        if(sComment == null){
            return -2;
        }
        // 当前发送删除请求的人不是发布评论的人
        if(sComment.getPostId() != userId){
            // 验证当前发送删除请求的人是不是art的作者
            PComment pComment = commentMapper.PComment(dto.getId());
            if(pComment == null){
                return -2;
            }
            Integer artUserId = artMapper.userIdByArt(pComment.getArtId());
            if(artUserId != userId){
                return -1;
            }
        }
        return commentMapper.delSComment(dto.getId());
    }
    public Integer delSCommentList(OpCommentDto dto){
        return commentMapper.delSCommentList(dto.getId());
    }
}
